//    Copyright (C) Kherty.  All rights reserved.
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.Parsing
{
    struct CompositeFormula
    {
        public CompositeFormula(string s)
        {
            Contract.Requires(s != null);
            int i = s.IndexOf('!');
            if (i > 0 && i < s.Length - 1)
            {
                string wsName = s.Substring(0, i);
                if (wsName.Length > 1 && wsName[0] == '\'' && wsName[wsName.Length - 1] == '\'')
                {
                    wsName = wsName.Substring(1, wsName.Length - 2).Replace("''", "'");
                }
                _worksheetName = wsName;

                Formula = s.Substring(i + 1, s.Length - i - 1);
            }
            else
            {
                _worksheetName = null;
                Formula = s;
            }
        }

        private string _worksheetName;
        public string WorksheetName
        {
            get { return _worksheetName; }
            set { _worksheetName = value; }
        }

        public bool IsError
        {
            get
            {
                switch (Formula)
                {
                    case "#REF!":
                        return true;
                    default: return false;
                }
            }
        }
        public string Formula;

        internal bool HasWorksheet
        {
            get
            { return WorksheetName != null; }
        }
    }
}
